import copy
import io
import json
import os
from urllib import parse


def checkStr2Int_Bool(ovalue):
    """
    检查字符串是否 数字、Boolean类型
    :param svalue:
    :return:
    """
    svalue = strStrip(ovalue)

    if svalue.isnumeric():
        # 不是引号包围的数字，才算数字
        return int(svalue) if not ovalue.strip().startswith("'") else svalue
    elif svalue.lower() in ['true', 'false']:
        return True if svalue.lower() == 'true' else False
    elif svalue.lower() in ['none','null']:
        return None
    else:
        return svalue


def strStrip(instr):
    """
    字符串首尾 去除空格、'、"、-
    :param instr:
    :return:
    """
    instr = instr.strip()
    if instr.startswith('-'):  # 去除 -
        instr = instr.replace('-', '', 1).strip()
    instr = instr.strip("'").strip()  # 去除 " ' 引号

    return instr


def json2File(outfile, obj):
    # jstr = json.dumps(obj, skipkeys=False, check_circular=True, allow_nan=True,
    #                   indent=4, separators=(',', ':'), ensure_ascii=False)
    with io.open(outfile, 'w', encoding='utf-8') as out:
        json.dump(obj, out, indent=2, separators=(',', ': '), ensure_ascii=False)
        out.write('\n')
    # print(jstr)


def loadJson(filename):
    with io.open(filename, 'r', encoding='utf-8') as jfile:
        return json.load(jfile)



def checkDuplDict(m1, m2):
    # 检查重复的字典 元素，并返回字典
    if isinstance(m1, dict):
        ret = {}
        for k, v in m1.items():
            for k2, v2 in m2.items():
                if k == k2 and v == v2:
                    ret[k] = v

        return ret
    elif isinstance(m1, list):
        ret = []
        for v in m1:
            for v2 in m2:
                if v == v2:
                    ret.append(v)
        return ret


def delDictItem(m1, m2):
    # 删除m2 dict 中与m1 一样的内容
    if isinstance(m1, dict):
        for k, v in m1.items():
            if k in m2:
                m2.pop(k)
        return m2
    elif isinstance(m1, list):
        for v in m1:
            if v in m2:
                m2.remove(v)
        return m2

def mergeDictList(m1,m2):
    #     合并两个字典 or list
    if isinstance(m1,dict):
        xtmp = delDictItem(m1, copy.deepcopy(m2))
        xtmp.update(m1)
        return xtmp
    elif isinstance(m1,list):
        xtmp = delDictItem(m1, m2.copy())
        xtmp.extend(m1)
        return xtmp

if __name__ == "__main__":
    file = 'D:\\zsah-git\\zsah_py_tester\\hrunEX\\reports\\reports\\shouba4.0 test on newdevgateway.shouba.cn\\20201021T020820.294919.html'
    url = 'http://192.168.0.243/reports/reports/'
    print('/'.join(file.replace('\\', '/').split('/')[-3:]))
    print(os.path.basename(os.path.abspath(file)))
    print(os.path.basename(file))

    if url.endswith("/"):  # 去除url尾部 /
        url = url[:-1]

    print("Report file URL = %s" % (parse.urljoin(url, parse.quote('/'.join(file.replace('\\', '/').split('/')[-3:])))))
